#include "objectlooper.h"

static uint16_t m_handleCount = 0;
static ObjectLooperHandler_t m_objectHandle[OBJECT_LOOPER_HANDLE_MAX];

void RegisterObjectLooper(void *object, uint16_t countDiv, ObjectLooperFunc func)
{    
    if(m_handleCount >= OBJECT_LOOPER_HANDLE_MAX)
    {
        return;
    }

    m_objectHandle[m_handleCount].Object = object;
    m_objectHandle[m_handleCount].LoopCount = countDiv;
    m_objectHandle[m_handleCount].Func = func;

    m_objectHandle[m_handleCount].CurrentCount = 0;

    m_handleCount++;
}

/// @brief 1ms循环
void ObjectLooperExec()
{
    int i;

    TIME_EXEC(1);
    
    for(i = 0; i < m_handleCount; i++) 
    {
        ObjectLooperHandler_t *h = &m_objectHandle[i];

        h->CurrentCount ++;
        if(h->CurrentCount >= h->LoopCount)
        {
            h->CurrentCount = 0;
            
            //加快速度
            // if(h->Object != NULL && h->Func != NULL)
            {
                h->Func(h->Object);
            }
        }
    }
}
